package com.nuc.zyt.day03.Lambda;

import com.nuc.zyt.day03.Lambda.pojo.Album;
import com.nuc.zyt.day03.Lambda.pojo.Artist;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @ClassName Stream
 * @Description TODO
 * @Author 耀酱
 * @Date 2023/5/23 15:23
 * @Version 1.0
 */

public class Stream1 {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("123456");
        list.add("123");
        list.add("1");

        Stream<String> stream = list.stream();
        System.out.println(stream);
        list.stream().filter(str -> str.length() > 1).forEach(value -> System.out.println(value));


        List<String> collect = Stream.of("ert", "ab", "hu").collect(Collectors.toList());
        for (String s : collect.stream().map(str -> str.toUpperCase()).collect(Collectors.toList())) {
            System.out.println(s);
        }
        System.out.println("_______________________");
        for (String s : Stream.of(list, collect).flatMap(str -> str.stream()).collect(Collectors.toList())) {
            System.out.println(s);
        }

        System.out.println("练习");
        System.out.println("计算一个流中所有数字之和");
        int i = addUp(Stream.of(1, 5, 6));
        System.out.println("相加结果为:" + i);

        System.out.println("自定义filter流");
        for (String s : miniFilter(list, str -> str.length() > 2)) {
            System.out.println(s);
        }


       // System.out.println("输出自定义对象的信息");




    }

    /**
     * 流中的数字相加
     *
     * @param numbers
     * @return
     */
    public static int addUp(Stream<Integer> numbers) {
        return numbers.reduce(0, (acc, element) -> acc += element);
    }

//    public static List<String> getArtist(List<Artist> list) {
//        return list.stream().filter(obj -> {
//            System.out.println(obj.getName());
//            System.out.println(obj.getOrigin());
//
//        }).collect(Collectors.toList());
//    }

    /**
     * 遍历每个元素，符合条件则加入新的集合
     * @param lists
     * @param predicate
     * @param <T>
     * @return
     */
    public static <T> List<T> miniFilter(List<T> lists, Predicate<? super  T> predicate){
        List<T> list = new ArrayList<>();
        for(T t: lists){
            if(predicate.test(t)){
                list.add(t);
            }
        }
        return list;
    }

    public static Stream miniMap(){
        return null;
    }
}